﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Hired
{
    /*  A thread posted on the Wall{?} of a user.
     *  This thread can have many comments under it.    */
    public class DBThread
    {
        public static bool AddThread(Thread thread)
        {
            try
            {
                thread.ThreadID = "tempvalue";
                thread.Timestamp = DateTime.Now;
                using (hireddbEntities db = new hireddbEntities())
                {
                    db.AddToThreads(thread);
                    db.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static Thread RetrieveThread(string threadID)
        {
            Thread thread = new Thread();
            using (hireddbEntities db = new hireddbEntities())
            {
                    thread =
                    (from t in db.Threads
                    where t.ThreadID == threadID
                    select t).SingleOrDefault();
            }
            return thread;
        }

        public static List<Thread> RetrieveThreadsByUserID(string ID)
        {
            using (hireddbEntities db = new hireddbEntities())
            {
                return
                    (from t in db.Threads.Include("Comments")
                     where t.WallID == ID
                     orderby t.Timestamp descending
                     select t).ToList();
            }
        }

        public static bool UpdateThread(Thread thd)
        {
            try
            {
                using (hireddbEntities db = new hireddbEntities())
                {
                    Thread threadToUpdate = RetrieveThread(thd.ThreadID);
                    db.Attach(threadToUpdate);
                    threadToUpdate.Comments = thd.Comments;
                    threadToUpdate.Contents = thd.Contents;
                    threadToUpdate.Timestamp = thd.Timestamp;
                    threadToUpdate.WallID = thd.WallID;
                    threadToUpdate.PosterID = thd.PosterID;
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool DeleteThread(Thread thd)
        {
            try
            {
                using (hireddbEntities db = new hireddbEntities())
                {
                    Thread threadToDelete = RetrieveThread(thd.ThreadID);
                    db.Attach(threadToDelete);
                    db.Threads.DeleteObject(threadToDelete);
                    db.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public static bool DeleteThreadbyId(string id)
        {
            try
            {
                using (hireddbEntities db = new hireddbEntities())
                {
                    Thread threadToDelete = RetrieveThread(id);
                    db.Attach(threadToDelete);
                    db.Threads.DeleteObject(threadToDelete);
                    db.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}